package indi.woniu.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import indi.woniu.entity.*;
import indi.woniu.mapper.IssueMapper;
import indi.woniu.service.IssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.util.ArrayList;
import java.util.List;

@Service
public class issueServiceImpl implements IssueService {
    @Autowired(required = false)
    private IssueMapper issueMapper;
    @Override
    public Integer addOne(QuestionDTO questionDTO) {
        //增加一个题目，需要增加该题，然后拿到试题id增加中间表，根据科目年级拿到字体库id
        //先增加答案，根据增加后的答案id设置answerid
        Answer answer = new Answer();
        answer.setAnswer(questionDTO.getAnswer().get("answer").asText());
        answer.setOptions(questionDTO.getAnswer().get("options").toString());
        answer.setDetail(questionDTO.getAnswer().get("detail").asText());
        issueMapper.addAnswer(answer);
        questionDTO.setAnswerId((int) answer.getId());
        Integer add = issueMapper.addOne(questionDTO);
        List<Integer> pids =null;
        if(questionDTO.getPool().getPoolName().equals("")){
            //有可能科目为空,查询出字库的id们
            pids = issueMapper.findByPoolName(questionDTO.getSonPool().getPoolName());
        }else{
            //有可能几个年级都可以做这个题
            pids = issueMapper.findBySubjectAndGrade(questionDTO.getPool().getPoolName(), questionDTO.getSonPool().getPoolName());
        }
        //拿到两个id，再往中间表添加改题目
        for (Integer pid : pids) {
            issueMapper.addPoolAndIssueId(questionDTO.getId(),pid);
        }
        //返回一个添加成功的信息
        return add;
    }

    @Override
    public Integer delOne(int id) {
        //删除自己这道题的答案
        issueMapper.delAnswer(id);
        //删除自己再题目表中的记录
        Integer del = issueMapper.delOne(id);
        //再删除自己与其他题库的记录
        Integer num = issueMapper.delPoolAndIssueId(id);
        System.out.println(num);
        return del;
    }

    @Override
    public ResponseEntity<PageInfo<QuestionDTO>> findByCondition(PerPage<QuestionDTO> perPage) {
        PageHelper.startPage(perPage.getPage(), perPage.getLimit());
        List<QuestionDTO> list= issueMapper.findByCondition(perPage.getData());
        PageInfo<QuestionDTO> questionDTOPageInfo = new PageInfo<>(list);
        return new ResponseEntity<PageInfo<QuestionDTO>>(200,"分页查询数据成功",questionDTOPageInfo);
    }


    @Override
    public ResponseEntity<PageInfo<QuestionDTO>> findByCond(PerPage<QuestionDTO> perPage) {
        List<QuestionDTO> questionDTOS = null;
        QuestionDTO questionDTO = perPage.getData();
        //科目库不为空,查询到科目的id，并根据科目id查询到所有的子年级库,再多表联查出
        if(!questionDTO.getPool().getPoolName().equals("")){
            int fid = issueMapper.findBySubject(questionDTO.getPool().getPoolName());
            List pids =new ArrayList();
            //
            //年级库是为空,为空全查
            if(questionDTO.getSonPool().getPoolName().equals("")){
                pids = issueMapper.findByFid(fid);
            }else{
                //年级库不为空，就传入年级库名和父亲库id,找到该科目库下面的题目
                int sonPool_id = issueMapper.findByFidAndName(fid,questionDTO.getSonPool().getPoolName());
                pids.add(sonPool_id);
            }
            //
            PageHelper.startPage(perPage.getPage(), perPage.getLimit());
            questionDTOS = issueMapper.findByPids(pids,questionDTO);
            PageInfo<QuestionDTO> questionDTOPageInfo = new PageInfo<>(questionDTOS);
            return new ResponseEntity<PageInfo<QuestionDTO>>(200,"分页查询数据成功",questionDTOPageInfo);
        }else{
            //科目库为空，年级库不为空，查询所有的年级库符合用户选择的年级
            List pids = issueMapper.findByPoolName(questionDTO.getSonPool().getPoolName());

            PageHelper.startPage(perPage.getPage(), perPage.getLimit());
            questionDTOS = issueMapper.findByPids(pids,questionDTO);
            PageInfo<QuestionDTO> questionDTOPageInfo = new PageInfo<>(questionDTOS);
            return new ResponseEntity<PageInfo<QuestionDTO>>(200,"分页查询数据成功",questionDTOPageInfo);
        }
    }

    @Override
    public Integer updateOne(QuestionDTO questionDTO) {
        //将该题目的id获取到，删除该题，删除该题与其他题库的连接，删除该题的原来答案
        issueMapper.delAnswer(questionDTO.getId());
        issueMapper.delOne(questionDTO.getId());
        issueMapper.delPoolAndIssueId(questionDTO.getId());
        //增加该题的答案，
        Answer answer = new Answer();
        answer.setAnswer(questionDTO.getAnswer().get("answer").asText());
        answer.setOptions(questionDTO.getAnswer().get("options").toString());
        answer.setDetail(questionDTO.getAnswer().get("detail").asText());
        issueMapper.addAnswer(answer);
        questionDTO.setAnswerId((int) answer.getId());
        //增加新连接，增加该题与题库(科目与年级查出id)的联系
        Integer add = issueMapper.addOne(questionDTO);
        //如果科目库为空，将其与所有科目的年级库连接
        List<Integer> pids = null;
        if(questionDTO.getPool().getPoolName().equals("")){
            pids = issueMapper.findByGrade(questionDTO.getSonPool().getPoolName());
        }else{
            pids = issueMapper.findBySubjectAndGrade(questionDTO.getPool().getPoolName(), questionDTO.getSonPool().getPoolName());
        }
        //添加该题与年级题库的连接
        for (Integer pid : pids) {
            issueMapper.addPoolAndIssueId(questionDTO.getId(),pid);
        }
        return add;
    }

    @Override
    public List<QuestionDTO> findAll() {
        return issueMapper.findAll();
    }

    @Override
    public QuestionDTO findById(int id) {
        //查询出科目，和其他信息
        List<String> subjects = issueMapper.findByIssueId(id);
        QuestionDTO questionDTOS = issueMapper.findInfoByIssueId(id);
        Pool pool = new Pool();
        pool.setPoolName(subjects.get(0));
        questionDTOS.setPool(pool);
        return questionDTOS;
    }
}
